package iscript.gameFramework.IsoLibarys.data 
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.utils.Dictionary;
	import iscript.coreFramework.data.HashTable;
	import iscript.coreFramework.displays.Element;
	import iscript.gameFramework.IsoLibarys.IsoScene;
	import iscript.gameFramework.IsoLibarys.IsoView;

	public class IsoViewManager  extends EventDispatcher
	{
		
		private var _isosceneList:HashTable = new HashTable();
	
		public function get count():int { return _isosceneList.size; }
		public function get source():HashTable { return _isosceneList}
		
		public function add(key:String,isoview:IsoView):Boolean {
				return  _isosceneList.add(key, isoview);
			}

			
		/**
		 * isoview Class  or  key String
		 * @param	isoscene
		 * @param	target
		 */	
		public function remove(target:*):void {
			   if (target is String) {
				   _isosceneList.removeByKey(target);
				   }
			   if (target is IsoView) {
				   _isosceneList.removeByValue(target);
				   }
			  
			}
			
		public function getIsoView(key:String):IsoView {
             return _isosceneList.getValue(key) as IsoView;
			}
		 
		public function getAllIsoView():Array {
				return _isosceneList.getAllValues();
			} 
			
			
		/**
		 * isoview Class  or  key String
		 * @param	isoscene
		 * @param	key
		 */	
		public function activeIsoView(target:*):void {
			   var views:Array =  _isosceneList.getAllValues();
			   for each ( var item:IsoView in views) {
				      Element.unActive(item);
				   }
				   
			   var view:IsoView;
			   if (target is String) {
				      view = _isosceneList.getValue(target);
				   }
			   if (target is IsoView) {
				    view = target;
				   }
				   _activeIsoView = view;
				   Element.Active(view);
				   
				   view.getIsoScene().dispatchEvent(new Event(IsoScene.ACTIVEISOVIEW));
			}
			
		public function getIsoViewKeyName(isoview:IsoView):String {			      
				  return _isosceneList.getUniqueKey(isoview) as String;
			}
		 
		private var _activeIsoView:IsoView;	
		public function getActiveIsoView():IsoView {
			  return  _activeIsoView;
			}
			
		public function getUnActiveIsoView():Vector.<IsoView> {
			 var views:Vector.<IsoView> = new Vector.<IsoView>();
			 for each (var item:IsoView in _isosceneList.getAllValues()) {
				   if (item != _activeIsoView) {
					   views.push(item)
					   }
				}
			 return views;			
			}	
			

		public function dispose():void {
			 for each ( var isoview:IsoView  in _isosceneList.getAllValues()) {
				      isoview.dispose();
				     if (isoview.parent != null) {
						  isoview.parent.removeChild(isoview);
						 }
				 }
			     _isosceneList.clear();
			}
			
			
		 static public const  ACTIVEISOVIEW:String = "ActiveIsoView";
			
	}
}

